package com.bigger.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigger.constant.ErrorConstant;
import com.bigger.dto.OrderPageDTO;
import com.bigger.dto.UpdateOrderDTO;
import com.bigger.entity.AddressBook;
import com.bigger.entity.Order;
import com.bigger.entity.OrderDetail;
import com.bigger.exception.BaseException;
import com.bigger.mapper.AddressBookMapper;
import com.bigger.mapper.OrderDetailMapper;
import com.bigger.mapper.OrderMapper;
import com.bigger.mapper.UserMapper;
import com.bigger.result.PageResult;
import com.bigger.service.OrderService;
import com.bigger.vo.OrderDetailVO;
import com.bigger.vo.OrderVO;
import com.bigger.vo.TurnoverVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.stream.Stream;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public PageResult<OrderVO> page(OrderPageDTO orderPageDTO) {
        int page = (orderPageDTO.getPage() - 1) * orderPageDTO.getPageSize();
        orderPageDTO.setPage(page);
        List<OrderVO> page1 = orderMapper.page(orderPageDTO);
        Long total = orderMapper.selectCount(null);
        return new PageResult<>(total,page1);
    }

    @Override
    public void update(UpdateOrderDTO updateOrderDTO) {
        Order order=new Order();
        Integer status = updateOrderDTO.getStatus();
        if (!(status.equals(Order.FINISH)||status.equals(Order.CANCEL)||status.equals(Order.NO_PAY))){
            throw new BaseException(ErrorConstant.ABNORMAL_OPERATION);
        }
        BeanUtils.copyProperties(updateOrderDTO,order);
        orderMapper.updateById(order);
    }

    @Override
    public OrderDetailVO getById(Long id) {
        Order order = orderMapper.selectById(id);
        QueryWrapper<OrderDetail> wrapper=new QueryWrapper<OrderDetail>()
                .eq("order_id",id);
        List<OrderDetail> orderDetails = orderDetailMapper.selectList(wrapper);
        AddressBook addressBook = addressBookMapper.selectById(order.getAddressId());
        OrderDetailVO orderDetailVO=new OrderDetailVO(order.getOrderNumber(),order.getStatus(),order.getPrice(),orderDetails,addressBook);
        return orderDetailVO;
    }

    @Override
    public TurnoverVO getTurnover() {
        LocalDate now = LocalDate.now(ZoneId.of("Asia/Shanghai"));
        LocalDateTime start=LocalDateTime.of(
                now.with(TemporalAdjusters.firstDayOfMonth()) //获取当月的月初日期
                , LocalTime.MIN
        );
        LocalDateTime end=LocalDateTime.of(
                now.with(TemporalAdjusters.lastDayOfMonth()) //获取当月的月末日期
                , LocalTime.MAX
        );
        Double total=orderMapper.getTurnover(null,null);//查询总营业额额
        if (total==null){
            total=0.0;
        }
        Double month = orderMapper.getTurnover(start, end);//查询月营业额
        if (month==null){
            month=0.0;
        }
        return new TurnoverVO(total,month);
    }
}
